﻿package mortal.component.window
{
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import flash.display.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.interfaces.*;

    public class BaseWindow extends Window
    {
        protected var _winTitleName:String;
        protected var _titleIconName:String;
        protected var _winFlowerName:String;
        protected var _titleBmp:Bitmap;
        protected var _winFlowerLeft:GBitmap;
        protected var _winFlowerRight:GBitmap;
        protected var _title:DisplayObject;
        protected var _windowBg:ScaleBitmap;
        protected var _windowBgLeft:ScaleBitmap;
        protected var _windowBgRight:ScaleBitmap;
        protected var _windowUnderBgLeft:ScaleBitmap;
        protected var _windowUnderBgRight:ScaleBitmap;
        protected var _windowBgName:String;
        protected var _windowBgName2:String;
        protected var _windowUnderBgName:String;
        protected var _windowCenter:ScaleBitmap;
        protected var _windowCenter2:ScaleBitmap;
        protected var _windowLine:ScaleBitmap;
        protected var paddingBottom:int = 11;
        protected var paddingLeft:int = 9;
        protected var paddingRight:int = 7;
        protected var blurTop:int = 2;
        protected var blurBottom:int = 2;
        protected var blurLeft:int = 2;
        protected var blurRight:int = 2;
        private var _titleLabel:TextField;
        protected var _titleChange:Boolean = false;

        public function BaseWindow(param1:ILayer = null)
        {
            super(param1);
            return;
        }// end function

        override protected function configParams() : void
        {
            _contentX = this.blurLeft;
            _contentY = this.blurTop;
            return;
        }// end function

        override public function setSize(param1:Number, param2:Number) : void
        {
            super.setSize(param1 + this.blurLeft + this.blurRight, param2 + this.blurTop + this.blurBottom);
            return;
        }// end function

        override protected function updateSize() : void
        {
            super.updateSize();
            if (this._windowBg)
            {
                this._windowBg.setSize(this.width, this.height);
                this._windowBg.x = 0;
                this._windowBg.y = 0;
            }
            if (this._windowBgName2)
            {
                this._windowBgLeft.setSize(this.width / 2 + 5, this.height + 15);
                this._windowBgLeft.x = -4;
                this._windowBgLeft.y = -10;
                this._windowBgRight.setSize(this.width / 2 + 5, this.height + 15);
                this._windowBgRight.x = this._windowBgLeft.x + this._windowBgLeft.width;
                this._windowBgRight.y = -10;
            }
            if (this._windowUnderBgName)
            {
                this._windowUnderBgLeft.setSize(this.width / 2 + 5, this.height + 15);
                this._windowUnderBgLeft.x = -4;
                this._windowUnderBgLeft.y = -10;
                this._windowUnderBgRight.setSize(this.width / 2 + 5, this.height + 15);
                this._windowUnderBgRight.x = this._windowBgLeft.x + this._windowBgLeft.width;
                this._windowUnderBgRight.y = -10;
            }
            if (this._winFlowerLeft)
            {
                this._winFlowerLeft.x = 0;
            }
            if (this._winFlowerRight)
            {
                this._winFlowerRight.x = this.width - this._winFlowerRight.width;
            }
            this.updateWindowCenterSize();
            return;
        }// end function

        protected function updateWindowCenterSize() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            if (this._windowCenter)
            {
                _loc_1 = this.width - this.paddingLeft - this.paddingRight - this.blurLeft - this.blurRight;
                _loc_2 = this.height - _titleHeight - this.paddingBottom - this.blurTop - this.blurBottom;
                this._windowCenter.setSize(_loc_1, _loc_2);
                this._windowCenter.x = this.paddingLeft + this.blurLeft;
                this._windowCenter.y = _titleHeight + this.blurTop;
            }
            if (this._windowCenter2)
            {
                this._windowCenter2.x = this._windowCenter.x + 5;
                this._windowCenter2.y = this._windowCenter.y + 5;
                this._windowCenter2.width = this._windowCenter.width - 10;
                this._windowCenter2.height = this._windowCenter.height - 10;
            }
            if (this._windowLine)
            {
                this._windowLine.x = 2;
                this._windowLine.y = _titleHeight + this.blurTop - 7;
                this._windowLine.width = this.width - 4;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.setWindowBgName();
            this.setFlwerName();
            if (this._windowBgName)
            {
                this._windowBg = UIFactory.bg(0, 0, -1, -1, null, this._windowBgName);
                addChildAt(this._windowBg, 0);
            }
            if (this._windowBgName2)
            {
                this._windowBgLeft = UIFactory.bg(0, 0, -1, -1, null, this._windowBgName2 + "Left");
                this._windowBgRight = UIFactory.bg(0, 0, -1, -1, null, this._windowBgName2 + "Right");
                addChildAt(this._windowBgLeft, 0);
                addChildAt(this._windowBgRight, 0);
            }
            if (this._windowUnderBgName)
            {
                this._windowUnderBgLeft = UIFactory.bg(0, 0, -1, -1, null, this._windowUnderBgName + "Left");
                this._windowUnderBgRight = UIFactory.bg(0, 0, -1, -1, null, this._windowUnderBgName + "Right");
                addChildAt(this._windowUnderBgLeft, 0);
                addChildAt(this._windowUnderBgRight, 0);
            }
            if (this._winFlowerName)
            {
                this._winFlowerLeft = UIFactory.bitmap(this._winFlowerName + "Left", 0, 2, null);
                this._winFlowerRight = UIFactory.bitmap(this._winFlowerName + "Right", 0, 2, null);
                addChild(this._winFlowerLeft);
                addChild(this._winFlowerRight);
            }
            this.addWindowLine();
            this.setWindowCenter();
            if (this._windowCenter)
            {
                addChildAt(this._windowCenter, contentSprite.numChildren);
            }
            return;
        }// end function

        protected function setWindowBgName() : void
        {
            this._windowBgName = "";
            this._windowBgName2 = "WindowBg";
            this._windowUnderBgName = "WindowUnderBg";
            return;
        }// end function

        protected function setFlwerName() : void
        {
            this._winFlowerName = "WinFlower";
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = this.width - _closeBtn.width - 15 - _contentTopOf3DSprite.x + _contentX;
                _closeBtn.y = 7 - _contentTopOf3DSprite.y + _contentY;
            }
            return;
        }// end function

        private function resetTitle() : void
        {
            if (this._titleLabel)
            {
                this._titleLabel.setTextFormat(GlobalStyle.windowTitle);
                this._titleLabel.autoSize = TextFieldAutoSize.CENTER;
                this._titleLabel.htmlText = this._titleLabel.htmlText;
            }
            return;
        }// end function

        public function get title()
        {
            return this._title;
        }// end function

        public function set title(param1) : void
        {
            if (this._title && this._title.parent)
            {
                this._title.parent.removeChild(this._title);
                this._title = null;
            }
            if (param1 is DisplayObject)
            {
                this._winTitleName = DisplayObject(param1).name;
                this._title = param1;
                _titleSprite.addChild(this._title);
            }
            else if (param1 is String)
            {
                this._winTitleName = param1;
                this._titleLabel = new TextField();
                this._titleLabel.mouseEnabled = false;
                this._titleLabel.embedFonts = true;
                this._titleLabel.defaultTextFormat = GlobalStyle.windowTitle;
                this._titleLabel.selectable = false;
                this._titleLabel.filters = [FilterConst.titleFilter, FilterConst.titleShadowFilter, FilterConst.titleGradientBevelFilter];
                this._titleLabel.autoSize = TextFieldAutoSize.LEFT;
                this._titleLabel.htmlText = param1;
                this._titleLabel.antiAliasType = AntiAliasType.ADVANCED;
                this._titleLabel.sharpness = 100;
                this._titleLabel.gridFitType = GridFitType.SUBPIXEL;
                this._titleLabel.thickness = 100;
                this._title = this._titleLabel;
                _titleSprite.addChild(this._titleLabel);
                ResManager.instance.registerTitle(this.resetTitle);
            }
            _titleSprite.x = -_contentTopOf3DSprite.x + _contentX;
            _titleSprite.y = -_contentTopOf3DSprite.y + _contentY;
            this._titleChange = true;
            this.updateDisplayList();
            return;
        }// end function

        public function get winTitleName() : String
        {
            return this._winTitleName;
        }// end function

        public function set titleIcon(param1:String) : void
        {
            this._titleIconName = param1;
            if (!this._titleBmp)
            {
                this._titleBmp = UIFactory.bitmap();
            }
            _titleSprite.addChild(this._titleBmp);
            LoaderHelp.addResCallBack(ResFileConst.windowIcon, this.titleIconHander);
            return;
        }// end function

        protected function titleIconHander() : void
        {
            this._titleBmp.bitmapData = GlobalClass.getBitmapData(this._titleIconName);
            this._titleChange = true;
            this.updateDisplayList();
            return;
        }// end function

        protected function setWindowCenter() : void
        {
            return;
        }// end function

        protected function addWindowCenter2() : void
        {
            this._windowCenter2 = UIFactory.bg();
            this.addChild(this._windowCenter2);
            return;
        }// end function

        protected function addWindowLine() : void
        {
            return;
        }// end function

        override protected function updateDisplayList() : void
        {
            var _loc_1:Number = NaN;
            super.updateDisplayList();
            if (this._titleChange)
            {
                this._titleChange = false;
                _loc_1 = 68;
                if (this._titleBmp && this._titleBmp.bitmapData)
                {
                    this._titleBmp.x = 17;
                    this._titleBmp.y = 35 - this._titleBmp.height;
                    _loc_1 = this._titleBmp.width + 17 + 5;
                }
                if (this._title)
                {
                    this._title.y = 5;
                    this._title.x = (this.width - this._title.width) / 2;
                }
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.paddingBottom = 11;
            this.paddingLeft = 9;
            this.paddingRight = 7;
            this._windowBgName = "";
            this._windowBgName2 = "";
            this._windowUnderBgName = "";
            this.blurTop = 2;
            this.blurBottom = 2;
            this.blurLeft = 2;
            this.blurRight = 4;
            if (this._windowBg)
            {
                this._windowBg.dispose(param1);
                this._windowBg = null;
            }
            if (this._windowBgLeft)
            {
                this._windowBgLeft.dispose(param1);
                this._windowBgLeft = null;
            }
            if (this._windowBgRight)
            {
                this._windowBgRight.dispose(param1);
                this._windowBgRight = null;
            }
            if (this._winFlowerLeft)
            {
                this._winFlowerLeft.dispose(param1);
                this._winFlowerLeft = null;
            }
            if (this._windowUnderBgLeft)
            {
                this._windowUnderBgLeft.dispose(param1);
                this._windowUnderBgLeft = null;
            }
            if (this._windowUnderBgRight)
            {
                this._windowUnderBgRight.dispose(param1);
                this._windowUnderBgRight = null;
            }
            if (this._winFlowerRight)
            {
                this._winFlowerRight.dispose(param1);
                this._winFlowerRight = null;
            }
            if (this._windowCenter)
            {
                this._windowCenter.dispose(param1);
                this._windowCenter = null;
            }
            if (this._windowCenter2)
            {
                this._windowCenter2.dispose(param1);
                this._windowCenter2 = null;
            }
            if (this._windowLine)
            {
                this._windowLine.dispose(param1);
                this._windowLine = null;
            }
            super.disposeImpl(param1);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose();
            this._windowBg = null;
            this._windowCenter = null;
            this._windowLine = null;
            this._title = null;
            DisplayUtil.removeAllChild(_titleSprite);
            return;
        }// end function

    }
}
